package cn.itcast.dao.impl;

import cn.itcast.dao.RouteDao;
import cn.itcast.daomain.PageBean;
import cn.itcast.daomain.classification;
import cn.itcast.daomain.commodity;
import cn.itcast.service.RouteService;
import cn.itcast.util.JDBCUtils;
import com.alipay.api.domain.Template;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RouteDaoImpl implements RouteDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findTotalCount(int cid) {
        String sql;
        Integer integer;
        if (cid!=0){
            sql="SELECT COUNT(*) FROM commodity where classid=?";
            integer = template.queryForObject(sql,Integer.class,cid);
        }else {
            sql="SELECT COUNT(*) FROM commodity";
            integer = template.queryForObject(sql, Integer.class);
        }
        return integer;
    }

    @Override
    public List<commodity> findByPage(int cid,int currentPage, int pageSize) {
        String sql;
        List<commodity> query;
        if (cid!=0){
            sql="SELECT * FROM  commodity WHERE classid = ? LIMIT ? , ? ";
            query = template.query(sql, new BeanPropertyRowMapper<commodity>(commodity.class), cid, currentPage, pageSize);
        }else {
            sql="SELECT * FROM  commodity LIMIT ? , ? ";
            query =template.query(sql,new BeanPropertyRowMapper<commodity>(commodity.class),currentPage,pageSize);
        }
        return query;
    }

    @Override
    public List<classification> classifications() {
        String sql="select * from classification";
        return template.query(sql,new BeanPropertyRowMapper<classification>(classification.class));
    }
}
